package p2pcdnsim.tests;

import static org.junit.Assert.*;

import org.junit.Test;

import p2pcdnsim.cdn.LRUCaching;
import p2pcdnsim.cdn.ObjectContent;

import desmoj.core.simulator.SimTime;

public class TestLRUCaching {
	
	public LRUCaching<ObjectContent> lru = new LRUCaching<ObjectContent>();
	
	@Test
	public void testRetrieveObject() {
		this.lru.setCacheSize(10);
		SimTime timeStamp = new SimTime(0.0);
		
		//Teste
		ObjectContent content = new ObjectContent(1,3);
		this.lru.insertObject(content, timeStamp);
		
		assertNotNull(lru.retrieveObject(1, timeStamp));
		
		ObjectContent content2 = new ObjectContent(2,10);
		lru.insertObject(content2, timeStamp);
		
		assertNotNull(lru.retrieveObject(2, timeStamp));
		assertNull(lru.retrieveObject(1, timeStamp));
		
		//Teste
		this.lru = new LRUCaching<ObjectContent>();
		this.lru.setCacheSize(10);
		
		this.lru.insertObject(content, timeStamp);
		
		ObjectContent content3 = new ObjectContent(3,4);
		this.lru.insertObject(content3, timeStamp);
		
		assertNotNull(this.lru.retrieveObject(1, timeStamp));
		assertNotNull(this.lru.retrieveObject(3, timeStamp));
		
	}
	
	
	@Test
	public void testInsertObject() {
		SimTime timeStamp = new SimTime(0.0);
		
		//Novo teste
		this.lru = new LRUCaching<ObjectContent>();
		this.lru.setCacheSize(10);
		
		ObjectContent content = new ObjectContent(1,3);
		assertTrue(this.lru.insertObject(content, timeStamp));
		
		ObjectContent content2 = new ObjectContent(2,5);
		assertTrue(this.lru.insertObject(content, timeStamp));
		
		//Novo teste
		this.lru = new LRUCaching<ObjectContent>();
		this.lru.setCacheSize(10);
		
		ObjectContent content3 = new ObjectContent(3,11);
		assertFalse(lru.insertObject(content3, timeStamp));
		
		//Novo teste
		this.lru = new LRUCaching<ObjectContent>();
		this.lru.setCacheSize(10);
		
		ObjectContent content4 = new ObjectContent(4,6);
		assertTrue(this.lru.insertObject(content4, timeStamp));
		assertTrue(this.lru.insertObject(content2, timeStamp));
		assertNull(this.lru.retrieveObject(4, timeStamp));
		
		
	}

}
